//
//  BaseViewController.swift
//  MedicalBeauty
//
//  Created by Henry on 2018/3/12.
//  Copyright © 2018年 shen_h. All rights reserved.
//

import UIKit

import MJRefresh

class BaseViewController: UIViewController,UIGestureRecognizerDelegate {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.backgroundColor
        
        self.navigationController?.navigationBar.barTintColor = UIColor.init(hexColor: "#ffffff")
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.tintColor = UIColor.black
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17)]
        
        
        if self.navigationController?.viewControllers.count != 1 {
            self.setLeftBarButton(title: "", imageName: "icon_return", action: #selector(backBarButtonMethon))
        }
        
        let shadowImage = UIImage.imageWithColor(UIColor.clear)
        self.navigationController?.navigationBar.setBackgroundImage(shadowImage, for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage.imageWithColor(UIColor.viewLineColor)
        
        
    }
    
    deinit {
        print("\(NSStringFromClass(self.classForCoder))  deinit")
    }
    
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}
extension BaseViewController{
    @objc func setLeftBarButton(title:String?,imageName:String, action:Selector){
        if imageName.isEmpty {
            
            let item = UIBarButtonItem(title: title, style: .plain, target: self, action: action)
            item.tintColor = UIColor.white
            self.navigationItem.leftBarButtonItem = item
            
        }else {
            let item = UIBarButtonItem(image: UIImage(named:imageName), style: .plain, target: self, action: action)
            item.title = ""
            self.navigationItem.leftBarButtonItem = item
        }
    }
    
    @objc func setRightBarButton(title:String,imageName:String, action:Selector,titleColor:String){
        if imageName.isEmpty {
            let item = UIBarButtonItem(title: title, style: .plain, target: self, action: action)
            item.tintColor = UIColor(hexColor: titleColor)
            item.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15)], for: .normal)
            
            self.navigationItem.rightBarButtonItem = item
        }else {
            let item = UIBarButtonItem(image: UIImage(named:imageName), style: .plain, target: self, action: action)
            self.navigationItem.rightBarButtonItem = item
        }
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.navigationController!.viewControllers.count <= Int(1) {
            return false
        }
        return true
    }
    
    @objc func backBarButtonMethon(){
        self.navigationController?.popViewController(animated: true)
    }
    
    func pushWith(viewController ctrl:UIViewController){
        self.navigationController?.pushViewController(ctrl, animated: true)
    }
}





















